package com.wbs.controller;


import com.wbs.common.ApplicationContextHelper;
import com.wbs.common.JsonData;
import com.wbs.dao.SysAclModuleMapper;
import com.wbs.exception.ParamException;
import com.wbs.exception.PermissionException;
import com.wbs.model.SysAclModule;
import com.wbs.param.TestVO;
import com.wbs.util.BeanValidator;
import com.wbs.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.color.ProfileDataException;
import java.util.Map;

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {


    @RequestMapping("/validate.json")
    @ResponseBody
    public JsonData validate(TestVO voo) {
        log.info("validate");
        try {
            Map<String, String> map = BeanValidator.validateObject(voo);  // 自己写的校验
            if (map != null && map.entrySet().size() > 0) {
                // 有数据的时候
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    log.info("{}->{}", entry.getKey(), entry.getValue());
                }
            }

        } catch (Exception e) {

        }
        return JsonData.success("test validate");
    }


    @RequestMapping("/validate1.json")
    @ResponseBody
    public JsonData validate1(TestVO voo) throws ParamException {
        log.info("validate");
        BeanValidator.check(voo);
        return JsonData.success("test validate");
    }

    @RequestMapping("/validate2.json")
    @ResponseBody
    public JsonData validate2(TestVO voo) throws ParamException {
        log.info("validate");
        SysAclModuleMapper sysAclModuleMapper= ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        SysAclModule sysAclModule=sysAclModuleMapper.selectByPrimaryKey(1);
        log.info(JsonMapper.obj2String(sysAclModule));
        BeanValidator.check(voo);
        return JsonData.success("test validate");
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        log.info("hello");
        return "hello,wbs";
    }


    @RequestMapping("/hello1.json")
    @ResponseBody
    public JsonData hello1() {
        log.info("hello1111");
        // throw new PermissionException("test exception");
        throw new RuntimeException("test Exception");
        // return JsonData.success("hello,wbs");
    }
}
